//
//  AsyncImageView.m
//  LocalTravelIPhone
//
//  Created by zheng ji on 14-3-28.
//  Copyright (c) 2014年 jxw. All rights reserved.
//

#import "AsyncImageView.h"
@interface AsyncImageView()
{
   
}
 @property (nonatomic, strong) UIActivityIndicatorView	*spinner;
@property (nonatomic, strong) NSURLConnection			*connection;
@property (nonatomic, strong) NSMutableData				*data;



@property (nonatomic, assign) BOOL	isInResuableCell;
@end
@implementation AsyncImageView
@synthesize imageUrl,spinner,connection,data,isInResuableCell;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
		[self setup];
	}
	return self;
}
-(void)setup{
    self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
	spinner.frame = self.frame;
	spinner.center = self.center;
	spinner.hidesWhenStopped = TRUE;
	
	//CALayer *layer = self.layer;
	//layer.cornerRadius = 10;
	self.clipsToBounds = TRUE;
}
-(void)setImageUrl:(NSString *)url{
    if(!url){
        self.isInResuableCell=true;
        [connection cancel];
        imageUrl=url;
        self.image=nil;
        return ;
    }
    imageUrl=url;
    [self downloadImage];
    
}
-(void)downloadImage{
    [self.superview addSubview:spinner];
    [spinner startAnimating];
    
    
    NSString* localImagePath=[NSString stringWithFormat:@"%@/%@",[CommonConfigUtil getImageLocalPath],[imageUrl stringByReplacingOccurrencesOfString:@"/" withString:@"_"]];
    
    if([CommonConfigUtil existImageWithLocalPath:localImagePath]){
        
        if ( !isInResuableCell) {
			self.image = [[UIImage alloc] initWithContentsOfFile:localImagePath];
		} else {
			self.image = [UIImage imageWithContentsOfFile:localImagePath];
		}
       // self.image=[[UIImage alloc] initWithContentsOfFile:localImagePath];
    }else{
        //从网络下载图片
        self.data = [NSMutableData data];
       NSString* netImagePath=[NSString stringWithFormat:@"%@100_75/%@",[CommonConfigUtil getTongChenImageBaseUrl],imageUrl];
        
        
         NSData *imageData=[NSData dataWithContentsOfURL:[ NSURL URLWithString:netImagePath]];
        if(imageData !=nil && [imageData length]>0){
            
            self.image=[UIImage imageWithData:imageData];
            [self setNeedsLayout];
            [imageData writeToFile:localImagePath atomically:YES];
        }
        /*
        
        NSURL *url = [NSURL URLWithString:netImagePath];
        NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
        self.connection=  [[NSURLConnection alloc] initWithRequest:request delegate:self];
        */
    }
  
    [spinner stopAnimating];
}
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response{
    NSLog(@"dd");
    return nil;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)respons
{
    NSLog(@"dd");
}
-(void)dealloc{
    [connection cancel];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
